package com.listener.dao.impl;

import com.listener.dao.TypeDao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class TypeDaoImpl implements TypeDao {
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate=jdbcTemplate;
    }
    public List<Map<String, Object>> selectAllType() {
        String sql="select * from type";
       return this.jdbcTemplate.queryForList(sql);
    }

    public Integer selectIdByType(String type) {
        String sql="select id from type where type='"+type+"'";
        return (Integer) this.jdbcTemplate.queryForMap(sql).get("id");
    }

    public Map<String,String> selectAllTypeColor(){
        Map<String,String> typeColorMap=new HashMap<String,String>();
        String sql="select type,color from type_color join type on type.id=type_color.id";
        List<Map<String, Object>> typeColorList = this.jdbcTemplate.queryForList(sql);
        for (Map<String, Object> typeColor : typeColorList) {
            typeColorMap.put((String) typeColor.get("type"),(String)typeColor.get("color"));
        }
        return typeColorMap;
    }
    public String selectColorByType(String type) {
        String sql="select color from type_color where id="+selectIdByType(type);
        return (String) this.jdbcTemplate.queryForMap(sql).get("color");
    }

    public Integer setTypeDefense(String type1, String type2, String effect) {
        Integer typeId1=selectIdByType(type1);
        Integer typeId2=selectIdByType(type2);
        String sql="UPDATE type_defense "+
                "SET defense = CONCAT("+
                "SUBSTRING(defense, 1,"+typeId2+"-1),"+
                effect+", "+
                "SUBSTRING(defense, "+typeId2+"+1)"+
                ")"+
                "WHERE id="+typeId1;
        return this.jdbcTemplate.update(sql);
    }
    public List<Map<String,Object>> selectTypeDefense(String type){
        List<Map<String,Object>> effectList=new ArrayList<Map<String,Object>>();
        List<Map<String, Object>> typeList = selectAllType();
        Integer id=selectIdByType(type);
        String sql="select defense from type_defense where id="+id;
        String defenseString= (String) this.jdbcTemplate.queryForMap(sql).get("defense");
        for (Map<String, Object> typeMap : typeList) {
            Map<String,Object> effectMap=new HashMap<String,Object>();
            effectMap.put("type",typeMap.get("type"));
            Integer index= (Integer) typeMap.get("id");
            double effect=Double.parseDouble(defenseString.substring(index-1,index));
            effect/=2;
            effectMap.put("effect",effect);
            effectList.add(effectMap);
        }
        return effectList;
    }
    public List<Map<String,Object>> selectManyTypeDefense(String type1,String type2){
        List<Map<String, Object>> typeDefenseEffectList1 = selectTypeDefense(type1);
        List<Map<String, Object>> typeDefenseEffectList2 = selectTypeDefense(type2);
        for(int i=0;i<typeDefenseEffectList1.size();i++){
            Double effect= (Double) typeDefenseEffectList1.get(i).get("effect");
            effect*=(Double) typeDefenseEffectList2.get(i).get("effect");
            typeDefenseEffectList1.get(i).put("effect",effect);
        }
        return typeDefenseEffectList1;
    }
}
